package com.woniuxy.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.dto.*;
import com.woniuxy.entity.WarehouseTable;
import com.woniuxy.enums.CodeEnum;
import com.woniuxy.exception.WarehouseException;
import com.woniuxy.mapper.WarehouseTableMapper;
import com.woniuxy.param.ListWareHouseParam;
import com.woniuxy.param.SaveWarehouseParam;
import com.woniuxy.param.UpdateWarehouseParam;
import com.woniuxy.service.ShopTableService;
import com.woniuxy.service.WarehouseShopService;
import com.woniuxy.service.WarehouseTableService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.util.WoniuBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 仓库表 服务实现类
 * </p>
 *
 * @author zcx, ftl, gr, rhs
 * @since 2021-12-31
 */
@Service
public class WarehouseTableServiceImpl extends ServiceImpl<WarehouseTableMapper, WarehouseTable> implements WarehouseTableService {
    @Autowired
    private WarehouseShopService warehouseShopService;
    @Autowired
    private ShopTableService shopTableService;

    @Override
    public PageWarehouseDto listWareHouse(ListWareHouseParam listWareHouseParam) {
        QueryWrapper<WarehouseTable> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("warehouse_remove_status", 0);
        if (null != listWareHouseParam.getKeyword()) {
            queryWrapper.and(qw -> {
                qw.like("warehouse_name", listWareHouseParam.getKeyword()).or()
                        .like("warehouse_linkman", listWareHouseParam.getKeyword()).or()
                        .like("warehouse_phone", listWareHouseParam.getKeyword());

            });
        }
        if (!StringUtils.isEmpty(listWareHouseParam.getHouseStatus())) {
            queryWrapper.eq("shop_status", listWareHouseParam.getHouseStatus());
        }
        IPage<WarehouseTable> iPage = page(new Page<>(listWareHouseParam.getIndexPage(), listWareHouseParam.getPageSize()), queryWrapper);
        PageWarehouseDto pageWarehouseDto = new PageWarehouseDto();
        pageWarehouseDto.setIndexPage(iPage.getCurrent());
        pageWarehouseDto.setTotal(iPage.getTotal());
        pageWarehouseDto.setWarehouseDtoList(WoniuBeanUtils.copyList(iPage.getRecords(), WarehouseDto.class));
        for (WarehouseDto warehouseDto : pageWarehouseDto.getWarehouseDtoList()) {
            String s = "";
            List<WarehouseShopDto> warehouseShopDtos = warehouseShopService.listShopByHouseId(warehouseDto.getWarehouseId());
            for (int i = 0; i < warehouseShopDtos.size(); i++) {
                s += "," + warehouseShopDtos.get(i).getShopName();
            }

            s = s.replaceFirst(",", " ");

            warehouseDto.setShopName(s);
        }
        return pageWarehouseDto;
    }

    @Override
    public SaveWarehouseDto SaveWarehouse(SaveWarehouseParam saveWarehouseParam) {
        WarehouseTable one = getOne(new QueryWrapper<WarehouseTable>().eq("warehouse_name", saveWarehouseParam.getWarehouseName()));
        if (null != one) {
            throw new WarehouseException(CodeEnum.WAREHOUSE_SAVE_FAIL_1.getMessage(), CodeEnum.WAREHOUSE_SAVE_FAIL_1.getCode());
        }
        boolean save = save(WoniuBeanUtils.copyObject(saveWarehouseParam, WarehouseTable.class));
        if (save) {
            SaveWarehouseDto saveWarehouseDto = WoniuBeanUtils.copyObject(saveWarehouseParam, SaveWarehouseDto.class);
            return saveWarehouseDto;
        }
        throw new WarehouseException(CodeEnum.WAREHOUSE_SAVE_FAIL.getMessage(), CodeEnum.WAREHOUSE_SAVE_FAIL.getCode());
    }

    @Override
    public Boolean updateWarehouse(UpdateWarehouseParam updateWarehouseParam) {
        WarehouseTable wht = new WarehouseTable();
        wht.setWarehouseId(updateWarehouseParam.getWarehouseId());
        wht.setWarehouseName(updateWarehouseParam.getWarehouseName());
        wht.setWarehouseLinkman(updateWarehouseParam.getWarehouseLinkman());
        wht.setWarehousePhone(updateWarehouseParam.getWarehousePhone());
        wht.setWarehouseStatus(updateWarehouseParam.getWarehouseStatus());
        boolean b = updateById(wht);
        if (!b)
            throw new WarehouseException(CodeEnum.WAREHOUSE_UPDATE_FAIL_1.getMessage(), CodeEnum.WAREHOUSE_UPDATE_FAIL_1.getCode());
        return b;
    }

    @Override
    public Boolean delWarehouse(List<Integer> warhouseIds) {
        List<WarehouseTable> list = list(new QueryWrapper<WarehouseTable>().in("warehouse_Id", warhouseIds));
        if (null == list || list.size() <= 0)
            throw new WarehouseException(CodeEnum.WAREHOUSE_DELETE_FAIL_2.getMessage(), CodeEnum.WAREHOUSE_DELETE_FAIL_2.getCode());
        List<WarehouseTable> collect = list.stream().filter(general -> {
            Boolean flag = !(general.getWarehouseRemoveStatus() == 1);
            if (flag) general.setWarehouseRemoveStatus(1);
            return flag;
        }).collect(Collectors.toList());
        if (collect.size() != list.size())
            throw new WarehouseException(CodeEnum.WAREHOUSE_DELETE_FAIL_3.getMessage(), CodeEnum.WAREHOUSE_DELETE_FAIL_3.getCode());
        return updateBatchById(collect);
    }

    @Override
    public WarehouseDto getwarehouse(Integer id) {
        WarehouseTable table = getById(id);
        WarehouseDto warehouseDto = WoniuBeanUtils.copyObject(table, WarehouseDto.class);
        String s = "";
        List<WarehouseShopDto> warehouseShopDtos = warehouseShopService.listShopByHouseId(warehouseDto.getWarehouseId());
        for (int i = 0; i < warehouseShopDtos.size(); i++) {
            s += "," + warehouseShopDtos.get(i).getShopName();
        }

        s = s.replaceFirst(",", " ");

        warehouseDto.setShopName(s);
        return warehouseDto;
    }
}
